
/*
 *
 * Copyright (C) Microsoft Corporation.  All rights reserved
 * 
 * The custom form for outlook-CRM connectivity.
 *  
 */


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;


namespace CRMScenarios
{
    /// <summary>
    /// The custom form class.
    /// </summary>
    public partial class ActionsPane : Form
    {
        InsertFlds insFld = null;
        int WinFormPixelAdjustment = 9;
        Word.Application WdApplication = null;
        Word.Document WdDoc = null;
        Dictionary<int, ActionsPane> Mapping = null;
        int docHash = 0;

        /// <summary>
        /// Constructor. internally creates the insert field form and adds it.
        /// </summary>
        public ActionsPane()
        {
            InitializeComponent();
            this.SizeChanged += new EventHandler(ActionsPane_SizeChanged);
            insFld = new InsertFlds();
            this.Controls.Add(insFld);
            ActionsPane_SizeChanged(null, null);
        }
        /// <summary>
        /// Adjust the size of internal control. Hence we need this event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ActionsPane_SizeChanged(object sender, EventArgs e)
        {
            insFld.sizeChange(this.Size.Width - WinFormPixelAdjustment, this.Size.Height);
        }

        /// <summary>
        /// Nothing to be done as this is outlook. ie, no scenario to be changed.
        /// </summary>
        /// <param name="scenarioSelection"></param>
        /// <param name="entityType"></param>
        /// <param name="searchPattern"></param>
        /// <param name="be"></param>
        /// <param name="refresh"></param>
        public void changeScenario(int scenarioSelection, string entityType, string searchPattern, ref BusinessEntity be, RefreshOption refresh)
        {
            // nothing to do here.
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        /// <summary>
        /// Okay!! Get all set to go.
        /// Get the server URL, 
        /// Try to login,
        /// If successful, do all the event handling initialization.
        /// Initialize the internal insert field form.
        /// </summary>
        /// <param name="application"></param>
        /// <param name="activeOne"></param>
        public void InitializeControls(object application, object activeOne)
        {
	    // Flush the message queue.
            Application.DoEvents();

            // LOGIN HERE?
            string TargetServerURL = CacheManager.GerServerURL(string.Empty);
            bool login = CrmConnector.Login(TargetServerURL);
            if (login == false)
            {
                insFld.FlagErrorOrWarning(global::CRMScenarios.Resources.LoginFailure, InfoOptions.Error);
                return;
            }
            if (application is Word.Application)
            {
                WdApplication = (Word.Application)application;
                WdDoc = (Word.Document)activeOne;
                ((Word.DocumentEvents_Event)WdDoc).Close += new Microsoft.Office.Interop.Word.DocumentEvents_CloseEventHandler(PaneCloseEvent);     
                WdApplication.DocumentChange += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentChangeEventHandler(ActionsPane_DocumentChange);
            }
            insFld.InitializeControls(application, activeOne);
            this.Text = global::CRMScenarios.Resources.InsertButtonCaption;
        }

        /// <summary>
        /// Keep the mapping of mail <-> customform.
        /// </summary>
        /// <param name="docHashCode"></param>
        /// <param name="MappingFromDocToForm"></param>
        public void Store(int docHashCode, ref Dictionary<int, ActionsPane> MappingFromDocToForm)
        {
            Mapping = MappingFromDocToForm;
            docHash = docHashCode;
        }

        /// <summary>
        /// 
        /// </summary>
        void PaneCloseEvent()
        {
            this.Dispose(true);
        }

        /// <summary>
        /// 
        /// </summary>
        void Pane_Close_Event()
        {
            this.Dispose(true);
        }

        /// <summary>
        /// On change of document/mail. make sure that custom form is not doing a wierd hide and seek.
        /// </summary>
        void ActionsPane_DocumentChange()
        {
            if (WdApplication.ActiveDocument == WdDoc)
            {
                this.Visible = true;
                this.Show();
            }
            else
            {
                this.Visible = false;
                this.Hide();
            }
        }
    }
}